<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" v-show="showSearch" :inline="true">
      <el-form-item label="时间区间"  prop="timeRange">
        <el-date-picker
          v-model="queryParams.timeRange"
          type="daterange"
          unlink-panels
          value-format="yyyy-MM-dd"
          range-separator="至"
          clearable
          start-placeholder="开始日期"
          end-placeholder="结束日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button icon="el-icon-download" type="primary" size="mini" @click="exportData" target="_blank">导出</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading"
              :data="list"
              border
              size="mini"
              :summary-method="getSummaries"
              show-summary>
      <el-table-column type="index" label="序号" width="50" align="center"></el-table-column>
      <el-table-column label="片区" prop="deptName" :show-overflow-tooltip="true" align="center"/>
      <el-table-column label="立项完成数" prop="total" :show-overflow-tooltip="true" align="center"/>
      <el-table-column label="设计完成项目数" prop="designTotal" align="center"/>
      <el-table-column label="采购计划完成项目数" prop="procurementTotal" align="center"/>
      <el-table-column label="预算完成项目数" prop="budgetTotal" align="center" />
      <el-table-column label="承建商匹配完成项目数" prop="matchTotal" align="center"/>
      <el-table-column label="承建商合约完成项目数" prop="contractTotal" align="center"/>
      <el-table-column label="施工启动完成项目数" prop="startTotal" align="center"/>
      <el-table-column label="施工完成完成项目数" prop="buildCompleteTotal" align="center"/>
      <el-table-column label="竣工验收完成项目数" prop="completeTotal" align="center"/>
      <el-table-column label="结算完成项目数" prop="balanceTotal" align="center"/>
    </el-table>
  </div>
</template>

<script>
  import { listForm,exportDatas} from "./projectSummary.js";
  import BaseIndex from '@/views/base/BaseIndex'
  import {deepClone} from "@/utils";
  export default {
    name: "ReportManagementProjectSummary",
    extends: BaseIndex,
    data() {
      return {
        listHeight:0,
        zoneList:[],
        list:[],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          timeRange: undefined,
        },
        props:{
          value:'id',
          label:'label'
        }
      };
    },
    watch: {

    },
    mounted(){
      this.$nextTick(function () {
        this.$forceUpdate()
      })
    },
    created() {
      this.getList();
    },
    methods: {
      listForm,
      getList() {
        if (this.queryParams.timeRange && this.queryParams.timeRange.length){
          this.queryParams.startTime = this.queryParams.timeRange[0]
          this.queryParams.endTime = this.queryParams.timeRange[1]
        }
        this.loading = true;
        let deepClone1 = deepClone(this.queryParams);
        delete deepClone1.pageSize
        delete deepClone1.pageNum
        delete deepClone1.timeRange
        return listForm(deepClone1).then(
          response => {
            if(response && response.code ===0){
              if (response.data)this.list = response.data
            }else{
              this.msgError(response.msg)
            }
            this.loading = false;
          }
        ).catch(err=>{
          this.loading = false;
        });
      },
      // 导出文件
      exportData(){
        exportDatas(this.queryParams).then(res=>{
          let link = document.createElement('a');
          link.href = URL.createObjectURL(res);
          link.setAttribute('download','项目汇总表.xls');
          link.click();
          link = null;
        }).catch(err=>{
        });
      },
      getSummaries(param) {
        const { columns, data } = param;
        const sums = [];
        columns.forEach((column, index) => {
          if (index === 0) {
            sums[index] = '总计';
            return;
          }
          const values = data.map(item => Number(item[column.property]));
          if (!values.every(value => isNaN(value))) {
            sums[index] = values.reduce((prev, curr) => {
              const value = Number(curr);
              if (!isNaN(value)) {
                return prev + curr;
              } else {
                return prev;
              }
            }, 0);
            sums[index] += ' ';
          } else {
            sums[index] = '';
          }
        });

        return sums;
      }
    }
  };
</script>
